#include "app_message.h"
#include "cjson/cJSON.h"
#include "log/log.h"
#include <stdlib.h>
#include <string.h>
#include "app_common.h"

/**
 * @brief 将字符数组转换为json字符串
 * 
 * @param chars 字符数组
 * @param len 字符数组实际长度
 * @return char* json字符串 NULL表示转换失败
 */
char *app_message_charsToJson(char *chars, int len)
{
    // 从chars中解析出conn_type,id_len,mes_len,id,mes
    int conn_type = (int)chars[0];
    int id_len = (int)chars[1];
    int mes_len = (int)chars[2];
    // 检测字符数组长度是否合法
    if (len != 3 + id_len + mes_len)
    {
        log_error("字符数组的长度不正确!");
        return NULL;
    }
    // 解析id和mes
    char id[id_len];
    char mes[mes_len];
    memcpy(id, chars + 3, id_len);
    memcpy(mes, chars + 3 + id_len, mes_len);

    // 生成16进制的id_hex和mes_hex字符串
    char *id_hex = app_common_charsToHexstr(id, id_len);
    char *mes_hex = app_common_charsToHexstr(mes, mes_len);

    // 生成json字符串
    cJSON *root = cJSON_CreateObject();
    cJSON_AddNumberToObject(root, "conn_type", conn_type);
    cJSON_AddStringToObject(root, "id", id_hex);
    cJSON_AddStringToObject(root, "mes", mes_hex);
    char *json = cJSON_PrintUnformatted(root);
    //释放内存
    cJSON_Delete(root);
    free (id_hex);
    free (mes_hex);

    return json;
}

/**
 * @brief 将json字符串转换为字符数组
 * 
 * @param json json字符串
 * @param chars_buf 字符数组缓冲区
 * @param buf_size 字符数组缓冲区大小
 * @return int 实际转化的字符数组长度 -1表示转换失败
 */
int app_message_jsonToChars(char *json, char *chars_buf, int buf_size)
{
    // 解析json字符串得到conn_type,id_hexstr,mes_hexstr
    cJSON *root = cJSON_Parse(json);
    if (root == NULL)
    {
        log_error("json字符串解析失败!"); 
        return -1;
    }
    cJSON *conn_type_item = cJSON_GetObjectItem(root, "conn_type");
    cJSON *id_item = cJSON_GetObjectItem(root, "id");
    cJSON *mes_item = cJSON_GetObjectItem(root, "mes");
    if (!cJSON_IsNumber(conn_type_item) || !cJSON_IsString(id_item) || !cJSON_IsString(mes_item))
    {
        log_error("JSON 字段类型错误!");
        cJSON_Delete(root);
        return -1;
    }
    
    int conn_type = conn_type_item->valueint;
    char *id_hexstr = id_item->valuestring; 
    char *mes_hexstr = mes_item->valuestring;

    // 将id_hexstr和mes_hexstr转换为字符数组id和mes，并计算id_len和mes_len
    int id_len = 0;
    int mes_len = 0;
    char *id = app_common_hexstrToChars(id_hexstr, &id_len);
    char *mes = app_common_hexstrToChars(mes_hexstr, &mes_len);

    // 检测hexstr字符串转换是否成功
    if (id == NULL || mes == NULL)
    {
        log_error("hexstr字符串转换失败!");
        cJSON_Delete(root);
        free(id);
        free(mes);
        return -1;
    }
    

    // 计算实际转化的字符数组长度
    int chars_len = 3 + id_len + mes_len;
    //检测字符数组长度是否合法
    if (buf_size < chars_len)
    {
        log_error("字符数组容器大小不足!");
        cJSON_Delete(root);
        free(id);
        free(mes);
        return -1;
    }

    // 将conn_type,id_len,mes_len,id,mes写入chars_buf
    chars_buf[0] = (char)conn_type;
    chars_buf[1] = (char)id_len;
    chars_buf[2] = (char)mes_len;
    memcpy(chars_buf + 3, id, id_len);
    memcpy(chars_buf + 3 + id_len, mes, mes_len);

    // 释放内存
    cJSON_Delete(root);
    free(id);   
    free(mes);

    // 返回实际转化的字符数组长度
    return chars_len;
}
